/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TileInfoPanel.java
 *
 * Created on Jul 25, 2011, 3:21:21 PM
 */
package deatax.display.gui;

import deatax.Player;
import deatax.ResourceAmount;
import deatax.Settlement;
import deatax.Tile;
import deatax.network.Client;
import deatax.unit.UnitStack;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JWindow;

/**
 *
 * @author pson
 */
public class TileInfoPanel extends javax.swing.JPanel {

    Tile tile;
    Player p;
    JWindow popupWindow;

    /** Creates new form TileInfoPanel */
    public TileInfoPanel() {
        initComponents();
        setOpaque(false);
        setBackground(new Color(0, 0, 0, 0));
        scrlPanel.getViewport().setOpaque(false);
        scrlPanel.getViewport().setBackground(new Color(0, 0, 0, 0));
        terrainResPanel.setPadding(5);
        improveResPanel.setPadding(5);
        tile = null;
        p = null;
        popupWindow = new JWindow(Client.getData().getFrame());
        popupWindow.getContentPane().setLayout(new BorderLayout());
        popupWindow.getContentPane().setBackground(new Color(0,0,0,128));
        lblTerrain.addMouseListener(new TileInfoTerrainListener());
        lblImprovement.addMouseListener(new TileInfoImproveListener());
    }

    public void setDetails(Tile t, Player cp) {
        tile = t;
        p = cp;
        refresh();
    }

    public void refresh() {
        terrainResPanel.clearResources();
        improveResPanel.clearResources();
        pnlPopulation.clearResources();
        pnlUnits.removeAll();
        if (tile == null) {
            lblTerrain.setText("");
            lblOwner.setText("");
            lblImprovement.setText("");
        } else {
            lblTerrain.setText(tile.getTerrain().getName());
            for (ResourceAmount ra : tile.getTerrain().getBonuses()) {
                terrainResPanel.addResource(ra);
            }
            terrainResPanel.calculate();
            lblOwner.setText(tile.getOwner() == null ? "Neutral" : tile.getOwner().getName());
            lblOwner.setForeground(tile.getOwner() == null ? Color.gray : tile.getOwner().getColor());
            if (tile.getImprovement() == null) {
                lblImprovement.setText("");
            } else {
                pnlPopulation.addPopulation(tile.getImprovement() instanceof Settlement ? tile.getPopulation() : -1);
                lblImprovement.setText(tile.getImprovement().getName());
                for (ResourceAmount ra : tile.getImprovement().getBonuses()) {
                    improveResPanel.addResource(ra);
                }
            }
            improveResPanel.calculate();
            Player first = tile.getOwner() == null ? p : tile.getOwner();
            for (UnitStack stack : tile.getAllUnits(first)) {
                UnitInfoPanel uip = new UnitInfoPanel();
                uip.setDetails(stack);
                pnlUnits.add(uip);
            }

        }
        validate();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        terrainResPanel = new deatax.display.gui.ResourcesPanel();
        improveResPanel = new deatax.display.gui.ResourcesPanel();
        jPanel1 = new javax.swing.JPanel();
        lblOwner = new javax.swing.JLabel();
        pnlPopulation = new deatax.display.gui.ResourcesPanel();
        scrlPanel = new javax.swing.JScrollPane();
        pnlUnits = new javax.swing.JPanel();
        lblImprovement = new javax.swing.JLabel();
        lblTerrain = new javax.swing.JLabel();

        terrainResPanel.setPreferredSize(new java.awt.Dimension(300, 20));

        javax.swing.GroupLayout terrainResPanelLayout = new javax.swing.GroupLayout(terrainResPanel);
        terrainResPanel.setLayout(terrainResPanelLayout);
        terrainResPanelLayout.setHorizontalGroup(
            terrainResPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 191, Short.MAX_VALUE)
        );
        terrainResPanelLayout.setVerticalGroup(
            terrainResPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 17, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout improveResPanelLayout = new javax.swing.GroupLayout(improveResPanel);
        improveResPanel.setLayout(improveResPanelLayout);
        improveResPanelLayout.setHorizontalGroup(
            improveResPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 187, Short.MAX_VALUE)
        );
        improveResPanelLayout.setVerticalGroup(
            improveResPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 17, Short.MAX_VALUE)
        );

        setBackground(new java.awt.Color(204, 255, 204));

        jPanel1.setOpaque(false);

        lblOwner.setFont(lblOwner.getFont().deriveFont(lblOwner.getFont().getStyle() | java.awt.Font.BOLD, lblOwner.getFont().getSize()+9));
        lblOwner.setText(" ");
        lblOwner.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        javax.swing.GroupLayout pnlPopulationLayout = new javax.swing.GroupLayout(pnlPopulation);
        pnlPopulation.setLayout(pnlPopulationLayout);
        pnlPopulationLayout.setHorizontalGroup(
            pnlPopulationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 187, Short.MAX_VALUE)
        );
        pnlPopulationLayout.setVerticalGroup(
            pnlPopulationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 25, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(lblOwner, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlPopulation, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblOwner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlPopulation, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
        );

        scrlPanel.setBackground(new Color(0,0,0,0));
        scrlPanel.setBorder(null);
        scrlPanel.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        scrlPanel.setOpaque(false);

        pnlUnits.setBackground(new Color(0,0,0,0));
        pnlUnits.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
        scrlPanel.setViewportView(pnlUnits);

        lblImprovement.setFont(lblImprovement.getFont().deriveFont(lblImprovement.getFont().getStyle() | java.awt.Font.BOLD, lblImprovement.getFont().getSize()+3));
        lblImprovement.setText(" ");

        lblTerrain.setFont(lblTerrain.getFont().deriveFont(lblTerrain.getFont().getStyle() | java.awt.Font.BOLD, lblTerrain.getFont().getSize()+3));
        lblTerrain.setText(" ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(scrlPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblTerrain, javax.swing.GroupLayout.DEFAULT_SIZE, 4, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(lblImprovement, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(252, 252, 252)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTerrain, javax.swing.GroupLayout.DEFAULT_SIZE, 17, Short.MAX_VALUE)
                    .addComponent(lblImprovement, javax.swing.GroupLayout.DEFAULT_SIZE, 17, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrlPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private deatax.display.gui.ResourcesPanel improveResPanel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblImprovement;
    private javax.swing.JLabel lblOwner;
    private javax.swing.JLabel lblTerrain;
    private deatax.display.gui.ResourcesPanel pnlPopulation;
    private javax.swing.JPanel pnlUnits;
    private javax.swing.JScrollPane scrlPanel;
    private deatax.display.gui.ResourcesPanel terrainResPanel;
    // End of variables declaration//GEN-END:variables

    private class TileInfoTerrainListener extends MouseAdapter {

        @Override
        public void mouseEntered(MouseEvent e) {

            popupWindow.getContentPane().removeAll();
            if (tile != null) {
                popupWindow.getContentPane().add(tile.getTerrain().getPopup(), BorderLayout.CENTER);
                popupWindow.pack();
                Point p = lblTerrain.getLocationOnScreen();
                popupWindow.setLocation(p.x + lblTerrain.getWidth() + 3, p.y);
                popupWindow.setVisible(true);
            }
        }

        @Override
        public void mouseExited(MouseEvent e) {
            popupWindow.setVisible(false);
        }
    }
    
    private class TileInfoImproveListener extends MouseAdapter {

        @Override
        public void mouseEntered(MouseEvent e) {

            popupWindow.getContentPane().removeAll();
            if (tile != null && tile.getImprovement() != null) {
                ResourcesPanel rp = (ResourcesPanel)tile.getImprovement().getPopup();
                if (tile.getPopulation() > 0) {
                    rp.addPopulation(tile.getPopulation());
                } else {
                    rp.addPopulation(-1);
                }
                popupWindow.getContentPane().add(rp, BorderLayout.CENTER);
                popupWindow.pack();
                Point p = lblImprovement.getLocationOnScreen();
                popupWindow.setLocation(p.x + lblImprovement.getWidth() + 3, p.y);
                popupWindow.setVisible(true);
            }
        }

        @Override
        public void mouseExited(MouseEvent e) {
            popupWindow.setVisible(false);
        }
    }
}
